package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

public class ChatFragment extends Fragment {

    private String[] people = {"段坤", "浪坤", "万坤", "刘坤"};
    private String[] times = {"19:30 AM", "19:15 AM", "18:45 AM", "17:50 AM"}; // 添加时间数组
    private int[] imageIds = {
            R.drawable.duankun,
            R.drawable.langkun,
            R.drawable.wankun,
            R.drawable.liukun
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_chat, container, false);

        ListView listView = view.findViewById(R.id.list_view);
        ChatAdapter adapter = new ChatAdapter();
        listView.setAdapter(adapter);

        listView.setOnItemClickListener((parent, view1, position, id) -> {
            String selectedPerson = people[position];
            Intent intent = new Intent(getActivity(), ChatHistoryActivity.class);
            intent.putExtra("person_name", selectedPerson);
            intent.putExtra("image_id", imageIds[position]);
            startActivity(intent);
        });

        return view;
    }

    private class ChatAdapter extends ArrayAdapter<String> {
        ChatAdapter() {
            super(getActivity(), R.layout.list_item_chat, people);
        }

        @NonNull
        @Override
        public View getView(int position, View convertView, @NonNull ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(getActivity()).inflate(R.layout.list_item_chat, parent, false);
            }

            TextView nameTextView = convertView.findViewById(R.id.name_text_view);
            TextView timeTextView = convertView.findViewById(R.id.time_text_view); // 获取时间 TextView
            ImageView avatarImageView = convertView.findViewById(R.id.avatar_image_view);

            nameTextView.setText(people[position]);
            timeTextView.setText(times[position]); // 设置时间文本
            avatarImageView.setImageResource(imageIds[position]);

            return convertView;
        }
    }
}